package org.ss.psci.dao;

import java.util.List;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.LockMode;
import org.springframework.context.ApplicationContext;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
import org.ss.psci.entity.PmBid;
import org.ss.psci.entity.PmEnd;

/**
 * Data access object (DAO) for domain model class PmEnd.
 * 
 * @see org.ss.psci.entity.PmEnd
 * @author MyEclipse Persistence Tools
 */

public class PmEndDAO extends HibernateDaoSupport {
	private static final Log log = LogFactory.getLog(PmEndDAO.class);
	// property constants
	public static final String CONTRACT_NAME = "contractName";
	public static final String END_DATE = "endDate";
	public static final String END_PRICE = "endPrice";
	public static final String PROVE_PRICE = "provePrice";
	public static final String MANAGER = "manager";
	public static final String REMARK = "remark";
	public static final String STATUS = "status";
	public static final String PROJECTID = "projectId";

	public List findByProject(Object name) {
		return findByProperty(PROJECTID, name);
	}
	
	
	public PmEnd copyEntity(PmEnd dest, PmEnd orig) {
		if (dest == null || orig == null) {
			return new PmEnd();
		}if (orig.getContractName() != null) {
			dest.setContractName(orig.getContractName());
		}
		if (orig.getStatus() != null) {
			dest.setStatus(orig.getStatus());
		}
		if (orig.getEndDate() != null) {
			dest.setEndDate(orig.getEndDate());
		}
		if (orig.getEndPrice() != null) {
			dest.setEndPrice(orig.getEndPrice());
		}
		if (orig.getProvePrice() != null) {
			dest.setProvePrice(orig.getProvePrice());
		}
		if (orig.getStatus()!= null) {
			dest.setStatus(orig.getStatus());
		}
		if (orig.getRemark() != null) {
			dest.setRemark(orig.getRemark());
		}
		if (orig.getManager() != null) {
			dest.setManager(orig.getManager());
		}
	
		return dest;
	}
	protected void initDao() {
		// do nothing
	}

	public void save(PmEnd transientInstance) {
		log.debug("saving PmEnd instance");
		try {
			getHibernateTemplate().saveOrUpdate(transientInstance);
			log.debug("save successful");
		} catch (RuntimeException re) {
			log.error("save failed", re);
			throw re;
		}
	}

	public void delete(PmEnd persistentInstance) {
		log.debug("deleting PmEnd instance");
		try {
			getHibernateTemplate().delete(persistentInstance);
			log.debug("delete successful");
		} catch (RuntimeException re) {
			log.error("delete failed", re);
			throw re;
		}
	}

	public PmEnd findById(java.lang.Integer id) {
		log.debug("getting PmEnd instance with id: " + id);
		try {
			PmEnd instance = (PmEnd) getHibernateTemplate().get(
					"org.ss.psci.entity.PmEnd", id);
			return instance;
		} catch (RuntimeException re) {
			log.error("get failed", re);
			throw re;
		}
	}

	public List findByExample(PmEnd instance) {
		log.debug("finding PmEnd instance by example");
		try {
			List results = getHibernateTemplate().findByExample(instance);
			log.debug("find by example successful, result size: "
					+ results.size());
			return results;
		} catch (RuntimeException re) {
			log.error("find by example failed", re);
			throw re;
		}
	}

	public List findByProperty(String propertyName, Object value) {
		log.debug("finding PmEnd instance with property: " + propertyName
				+ ", value: " + value);
		try {
			String queryString = "from PmEnd as model where model."
					+ propertyName + "= ?";
			return getHibernateTemplate().find(queryString, value);
		} catch (RuntimeException re) {
			log.error("find by property name failed", re);
			throw re;
		}
	}

	public List findByContractName(Object contractName) {
		return findByProperty(CONTRACT_NAME, contractName);
	}

	public List findByEndDate(Object endDate) {
		return findByProperty(END_DATE, endDate);
	}

	public List findByEndPrice(Object endPrice) {
		return findByProperty(END_PRICE, endPrice);
	}

	public List findByProvePrice(Object provePrice) {
		return findByProperty(PROVE_PRICE, provePrice);
	}

	public List findByManager(Object manager) {
		return findByProperty(MANAGER, manager);
	}

	public List findByRemark(Object remark) {
		return findByProperty(REMARK, remark);
	}

	public List findByStatus(Object status) {
		return findByProperty(STATUS, status);
	}

	public List findAll() {
		log.debug("finding all PmEnd instances");
		try {
			String queryString = "from PmEnd";
			return getHibernateTemplate().find(queryString);
		} catch (RuntimeException re) {
			log.error("find all failed", re);
			throw re;
		}
	}

	public PmEnd merge(PmEnd detachedInstance) {
		log.debug("merging PmEnd instance");
		try {
			PmEnd result = (PmEnd) getHibernateTemplate().merge(
					detachedInstance);
			log.debug("merge successful");
			return result;
		} catch (RuntimeException re) {
			log.error("merge failed", re);
			throw re;
		}
	}

	public void attachDirty(PmEnd instance) {
		log.debug("attaching dirty PmEnd instance");
		try {
			getHibernateTemplate().saveOrUpdate(instance);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}

	public void attachClean(PmEnd instance) {
		log.debug("attaching clean PmEnd instance");
		try {
			getHibernateTemplate().lock(instance, LockMode.NONE);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}

	public static PmEndDAO getFromApplicationContext(ApplicationContext ctx) {
		return (PmEndDAO) ctx.getBean("PmEndDAO");
	}
}